package pit.com.ordermobilesystem.adapter;


import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import pit.com.ordermobilesystem.ShopActivity;
import pit.com.ordermobilesystem.R;
import pit.com.ordermobilesystem.constant.OrderConstant;
import pit.com.ordermobilesystem.fragment.BaseTabFragment;
import pit.com.ordermobilesystem.fragment.ItemFragment;
import pit.com.ordermobilesystem.imageutil.ImageFetcher;
import pit.com.ordermobilesystem.imageutil.ImageResizer;
import pit.com.ordermobilesystem.provider.BaseProvider;
import pit.com.ordermobilesystem.provider.OrderMobileProvider.CategoryTable;
import pit.com.ordermobilesystem.util.Utils;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class CategoryAdapter extends CursorAdapter{
	  private LayoutInflater mLayoutInflater;
	  private Context mContext;
	  public ImageLoader imageLoader = ImageLoader.getInstance();
	  Fragment frag;
	  DisplayImageOptions options;
	public CategoryAdapter(Context context, Cursor c,Fragment mfrag) {
		super(context, c,true);
		    mContext = context;
		    frag=mfrag;
		    mLayoutInflater = LayoutInflater.from(context);
			options = new DisplayImageOptions.Builder()
			.showImageOnLoading(R.drawable.ic_no_image)
			.showImageForEmptyUri(R.drawable.ic_no_image)
			.showImageOnFail(R.drawable.ic_no_image)
			.cacheInMemory(true)
			.cacheOnDisk(true)
			.considerExifParams(true)
			.bitmapConfig(Bitmap.Config.RGB_565)
			.build();		    
		    
		    
		    
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		CategoryView category_view=(CategoryView) view;
		category_view.tv_category.setText(cursor.getString(cursor.getColumnIndex(CategoryTable.NAME_CATEGORY)));
		String img_category=Utils.getPathImage( cursor.getString(cursor.getColumnIndex(CategoryTable.LINK_IMAGE)));
		if(img_category!=null && !img_category.equals("")){
			imageLoader.displayImage( img_category, category_view.img_category,options, null);
		}else{
			category_view.img_category.setImageResource(R.drawable.ic_no_image);	
		}	
		final int category_id=cursor.getInt(cursor.getColumnIndex(CategoryTable.ID_CATEGORY));
		final String category_name=cursor.getString(cursor.getColumnIndex(CategoryTable.NAME_CATEGORY));
		final Context mContext=context;
		view.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(frag instanceof BaseTabFragment){
					BaseTabFragment container=(BaseTabFragment) frag;
					ItemFragment itemFragment = new ItemFragment();
					itemFragment.setName("item");
					 Bundle bundle=new Bundle();
					 bundle.putInt("category_id", category_id);
					 bundle.putString("category_name", category_name);
					 itemFragment.setArguments(bundle);
					 container.addFragment(itemFragment);
				}
				
			}
		});
		
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		return new CategoryView(context);
	}
	
	public JSONObject getCurrentItem() throws JSONException{
		return BaseProvider.getJsonObjectFromCursor(getCursor());
		
		
	}
	
	class CategoryView extends RelativeLayout {
		TextView tv_category;
		ImageView img_category;

		public CategoryView(Context context) {
			super(context);
			LayoutInflater li = (LayoutInflater) this.getContext()
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			li.inflate(R.layout.view_category, this, true);
			tv_category = (TextView) findViewById(R.id.tv_category);
			img_category = (ImageView) findViewById(R.id.img_category);
		}
	}	

}
